//
//  E7iAd.mm
//  EndlessBattle
//
//  Created by cc on 12-3-26.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

#import "E7iAd.h"

CE7iAd* s_piAd = NULL ;

@interface E7iAdController : UIViewController <ADBannerViewDelegate>
{
    ADBannerView *adView ;
    BOOL bannerIsVisible ;
}

@property (nonatomic,assign) BOOL bannerIsVisible ;

@end

@implementation E7iAdController

@synthesize bannerIsVisible ;

static E7iAdController* _shareiAdController = nil ;

+ (E7iAdController*) shareiAdController
{
    if (nil == _shareiAdController)
    {
        _shareiAdController = [[E7iAdController alloc] init] ;
    }

    return _shareiAdController ;
}

- (void) dealloc {
    adView.delegate=nil;
    [adView release];
    [super dealloc];
    _shareiAdController = nil ;
}

- (void) viewDidLoad
{
//    NSLog(@"enter viewDidLoad");
//    [self loadView] ;
    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
//    adView.frame = CGRectOffset(adView.frame, 0, -50);
    adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierLandscape];
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;

    self.view = (UIView*)(CE7iAd::GetiAd()->GetView()) ;
//    presentModalViewController

    [self.view addSubview:adView];
    adView.delegate=self;
    self.bannerIsVisible=NO;
    [super viewDidLoad];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
//    NSLog(@"enter bannerViewDidLoadAd");
    if (!self.bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
        // banner is invisible now and moved out of the screen on 50 px
//        banner.frame = CGRectOffset(banner.frame, 0, 50);
        [UIView commitAnimations];
        self.bannerIsVisible = YES;
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
//    NSLog(@"enter bannerView");
    if (self.bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
        // banner is visible and we move it out of the screen, due to connection issue
//        banner.frame = CGRectOffset(banner.frame, 0, -50);
        [UIView commitAnimations];
        self.bannerIsVisible = NO;
    }
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
//    NSLog(@"enter bannerViewActionShouldBegin");
    BOOL shouldExecuteAction = YES;
    if (!willLeave && shouldExecuteAction)
    {
        // stop all interactive processes in the app
        // [video pause];
        // [audio pause];
    }
    return shouldExecuteAction;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
//    NSLog(@"enter bannerViewActionDidFinish");
    // resume everything you've stopped
    // [video resume];
    // [audio resume];
}

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
        adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
    else
        adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    printf("enter should rotate. --------\n") ;
    // Return YES for supported orientations
    return ((interfaceOrientation == UIInterfaceOrientationPortrait) | UIInterfaceOrientationPortrait);
}

@end


CE7iAd::CE7iAd()
{
}

CE7iAd::~CE7iAd()
{
}

CE7iAd* CE7iAd::GetiAd()
{
    if (NULL == s_piAd)
    {
        s_piAd = new CE7iAd() ;
    }

    return s_piAd ;
}

void CE7iAd::LoadView(void* pView)
{
    m_pView = pView ;
    printf("load view OK ...\n") ;
}

void CE7iAd::Create()
{
    [[E7iAdController shareiAdController] viewDidLoad] ;
}

void CE7iAd::Destroy()
{
    [[E7iAdController shareiAdController] dealloc] ;
}

void CE7iAd::Show()
{
}

void CE7iAd::Hide()
{
}

// util area.
bool IsPad()
{
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ;
}

void OpenURL(const char* url)
{
    NSString* ns_url = [NSString stringWithUTF8String:url] ;
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:ns_url]];
}















